<template>
	<view class="wrap">
		<uni-swipe-action ref="swipeAction">
			<uni-list v-if="list.length">
				<uni-swipe-action-item v-for="(v,i) in list" :key="v.clockId" :right-options="rightOptions"
					@click="swipeClick($event, v.clockId)">
					<uni-list-item :title="v.time?.slice(11)" />
				</uni-swipe-action-item>
			</uni-list>
		</uni-swipe-action>
		<!-- <button @click="deleteTable">删除库</button> -->

		
		<image @click="fabClick"  class="button" src="/static/point.png" alt="" srcset="" />
		
		<LogConsole ref="logRef" showLog />
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue';
	import LogConsole from "/components/yulong-VConsole/LogConsole.vue"
	import authenticate from '../../utils/authenticate';
	import moment from 'moment';
	import {
		addDateRecord,
		queryDateId
	} from '../../sqDB/date';
	import logger, {
		initLogger
	} from '../../utils/logger';
	import {
		addClockRecord,
		deleteClockRecord,
		queryClockRecord
	} from '../../sqDB/clock';
	import initDB from '../../sqDB/initDB';
	import {
		dbName,
		dbPath
	} from '../../sqDB/constant';
	const rightOptions = [{
		text: '删除',
		style: {
			backgroundColor: '#FF586A',
			color: '#ffffff',
		}
	}]
	const dateId = ref(null);
	const swipeClick = async (e, clockId) => {
		switch (e.index) {
			case 0:
				const res = await authenticate();
				if (res === null) return;
				const deleteRes = await deleteClockRecord(clockId);
				if (deleteRes) {
					getList(dateId.value);
				}
				break;
			default:
				break;
		}
	}
	const deleteTable = async () => {
		const res = await authenticate();
		if (res === null) return;
		plus.io.resolveLocalFileSystemURL(dbPath, (entry) => {
			entry.remove(() => {
				logger(`数据库 ${dbName} 删除成功`);
			}, (e) => {
				logger(`删除数据库文件失败:`, e);
			});
		}, () => {
			logger(`数据库文件 ${dbPath} 不存在`);
		});

		// plus.sqlite.executeSql({
		// 	name: dbName,
		// 	sql: `DROP TABLE IF EXISTS clocks`,
		// 	success: () => {
		// 		logger(`表 clocks 删除成功`);
		// 		resolve();
		// 	},
		// 	fail: (e) => {
		// 		logger(`删除表clocks 失败:`, e);
		// 		reject(e);
		// 	}
		// });
	}
	const list = ref([{
			"clockId": 1,
			"dateId": 1,
			"time": "2025-05-21 11:15:37"
		},
		{
			"clockId": 2,
			"dateId": 1,
			"time": "2025-05-21 11:15:39"
		},
		{
			"clockId": 3,
			"dateId": 1,
			"time": "2025-05-21 11:15:42"
		}
	])
	

	const getList = async (dateId) => {
		const res = await queryClockRecord(dateId);
		logger('getList', res)
		list.value = res ?? [];
	}
	const fabClick = async () => {
		const res = await authenticate();
		if (res === null) {
			uni.showToast({
				icon: 'none',
				title: `取消打卡`,
			})
			return
		}
		uni.showToast({
			icon: 'none',
			title: `打卡${res ? '成功' : '失败'}`,
		})
		if (res) {
			await addClockRecord({
				dateId: dateId.value,
				time: moment().format('YYYY-MM-DD HH:mm:ss')
			});
			getList(dateId.value);
		}

	}

	const logRef = ref(null);
	
	const handleBtn = () => {
		logRef.value.log('handleBtn click');
	}
	onMounted(async () => {
		initLogger(logRef.value.log);
		if (uni.getSystemInfoSync().osName !== 'android') {
			return;
		}
		await initDB();
		const date = moment().format('YYYY-MM-DD')
		const res = await queryDateId(date);
		if (typeof res !== 'number') {
			dateId.value = await addDateRecord(date);
		}else{
			dateId.value = res;
		}
		getList(dateId.value)
	})
</script>

<style scoped lang="scss">
	.wrap {
		padding: 15px;
		.button{
			width: 50px;
			height: 50px;
			position: fixed;
			bottom: 140px;
			left: 50%;
			transform: translateX(-50%);
		}
	}
</style>